package main

import (
	"context"
	"fmt"
	"log"
	"time"

	colly "github.com/gocolly/colly/v2"
	"github.com/gocolly/colly/v2/debug"
	"github.com/gocolly/colly/v2/extensions"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type People struct {
	Name         string                `json:"name"`
	CommonInfo   string                `json:"commonInfo"`
	Age          int                   `json:"age"`
	WorkYear     int                   `json:"workYear"`
	Education    string                `json:"education"`
	WorkingState string                `json:"workingState"`
	Expectation  string                `json:"expectation"`
	Advantage    string                `json:"advantage"`
	ActiveDate   string                `json:"activeDate"`
	History      []OccupationalHistory `json:"history"`
}
type OccupationalHistory struct {
	Time    string `json:"time"`
	Company string `json:"company"`
	Post    string `json:"post"`
}

var (
	dberr      error
	client     *mongo.Client
	db         *mongo.Database
	collection *mongo.Collection
	Datas      []interface{}
)

func InsertMany(client *mongo.Client, data []interface{}) {
	collection := client.Database("aqie").Collection("boss")
	ids, _ := collection.InsertMany(context.TODO(), data)
	if ids != nil {
		// fmt.Printf("mongo:[%s]", ids)
	}
}
func main() {
	//数据库连接
	ctx, cancelFunc := context.WithCancel(context.TODO())
	time.AfterFunc(10*time.Second, func() {
		cancelFunc()
	})
	if client, dberr = mongo.Connect(ctx, options.Client().ApplyURI("mongodb://localhost:27017").SetConnectTimeout(5*time.Second)); dberr != nil {
		fmt.Print(dberr)
		return
	} else {
		log.Println("mongodb connected")
	}

	// mUrl := "https://www.zhipin.com/web/boss/index"
	mUrl := "https://www.zhipin.com/web/boss/recommend"
	//colly的主体是Collector对象，管理网络通信和负责在作业运行时执行附加的回掉函数

	c := colly.NewCollector(func(collector *colly.Collector) {
		collector.Async = true
		collector.SetDebugger(&debug.LogDebugger{})
		extensions.RandomUserAgent(collector)
	})
	// 设置请求头
	c.OnRequest(func(r *colly.Request) {
		r.Headers.Set("Cookie", "wd_guid=9de1efac-73d0-4b2e-85d2-238297e95391; historyState=state; _bl_uid=4tlgm6awr5Cn00ce4oIL3pb6qI9L; __g=-; Hm_lvt_194df3105ad7148dcf2b98a91b5e727a=1663462750,1665361997; wt2=DmRIo7TuCs3yqgc9KtgKSIKduO_pSAvTwjoZDOfhZd1P9FKBZJJt7dSQYLA5dVyACxcKi0aQlAm8gy8OBz76_TQ~~; wbg=1; __l=l=%2Fwww.zhipin.com%2Fweb%2Fboss%2Frecommend&r=&g=&s=3&friend_source=0&s=3&friend_source=0; geek_zp_token=V1Q9wgGOH62FxgXdNvyhgcKyK27DzXwA~~; Hm_lpvt_194df3105ad7148dcf2b98a91b5e727a=1665542421; __c=1665361992; __a=35877707.1657174256.1663462747.1665361992.143.11.24.143; zp_token=V1Q9wgGOH62FxgXdNvyhgcLCqx6zrfxQ%7E%7E")
		r.Headers.Add("referer", "https://www.zhipin.com/web/boss/recommend")
		r.Headers.Add("sec-fetch-mode", "cors")
		r.Headers.Add("sec-fetch-site", "same-origin")
		r.Headers.Add("token", "pP9mfrTQ9VX6PXY")
		r.Headers.Add("traceid", "F5A9C376-E2E5-423B-A5F3-6FA2002B415A")
		r.Headers.Add("zp_token", "V1Q9wgGOH62FxgXdNvyhgcLCqx6zrfxQ~~")
	})
	//发送请求错误被回调
	c.OnError(func(_ *colly.Response, err error) {
		fmt.Print(err)
	})

	//响应请求之后被回调
	c.OnResponse(func(r *colly.Response) {
		fmt.Println("Response body length：", len(r.Body))
	})

	// #recommend-list > div > ul > li:nth-child(2)
	c.OnHTML("recommend-list > div > ul > li", func(e *colly.HTMLElement) {
		people := People{
			// #recommend-list > div > ul > li:nth-child(2) > div > div.card-inner.common-wrap.checked > div.col-2 > div.row.name-wrap > span.name
			Name:       e.ChildText("div > div.card-inner.common-wrap.checked > div.col-2 > div.row.name-wrap > span.name"),
			CommonInfo: e.ChildText("div > div.card-inner.common-wrap.checked > div.col-2 > div.join-text-wrap.row.base-info"),
		}

		log.Println("people", people)
		Datas = append(Datas, people)
	})

	//在OnHTML之后被调用
	c.OnScraped(func(r *colly.Response) {
		fmt.Println("Finished", r.Request.URL)
	})
	//这里是执行访问url
	c.Visit(mUrl)
	InsertMany(client, Datas)
}
